(function () {
    var io = ppt.io;
    function RoleController() {
        RoleController.super(this);
        this.ASSETS = {
            RES: ASSETS.ROLESERES,
            TYPE: "simple"
        }
        this.BG = null;
        this.ui = null;
        this._role = [];
        this._details = [];
        this.beginPosition = [];
        this.endPosition = [];
        this._roleBox = null;
        this._choose = [];
        this._haveRole = [];
        this._len = 4;
        this._roleId = [];
        this._status = [];
        this._roleIndex = null;
        this.direction = null;
        this.init();
    }
    Laya.class(RoleController, "Scene.Role", Laya.Sprite);
    var _proto = RoleController.prototype;
    _proto.destroy = function () {
        Laya.Sprite.prototype.destroy.call(this, true);
        this.BG = null;
        this.ui = null;
        this._role = null
        this._details = null
        this.beginPosition = null
        this.endPosition = null
        this._roleBox = null;
        this._choose = null
        this._haveRole = null
        this._len = null;
        this._roleId = null;
        this._status = null;
        this._roleIndex = null;
        this.direction = null;
    }
    _proto.init = function () {
        this.size(GAME_CONFIG.WIDTH, Laya.stage.height);
        io.register("role::roleList", this, this.changeData);
        io.register("role::press", this, this.upData);
    }
    _proto.createUI = function () {
        var BG = utils.createSkeleton('res/public/public_bg');
        BG.pos(GAME_CONFIG.WIDTH / 2, Laya.stage.height / 2);
        var roletab = new Com.Role.Tab(function (direction) {
            this.direction = direction;
            this.fieldScroll();
        }.bind(this));
        roletab.y = Laya.stage.height / 2 - roletab.height / 2;
        this.ui = roletab;
        this.ui.zOrder = 1;
        this.BG = BG;
        BG.play('public_bg', true);
        this.addChildren(BG, roletab);
        var figure = [];
        var figureDetails = [];
        var figureHave = [];
        figure[0] = utils.createSkeleton('other/role/normal/1001');
        figureHave[0] = utils.createSkeleton('other/role/normal/1001');
        figure[1] = utils.createSkeleton('other/role/normal/1002');
        figureHave[1] = utils.createSkeleton('other/role/normal/1002');
        figure[2] = utils.createSkeleton('other/role/normal/1003');
        figureHave[2] = utils.createSkeleton('other/role/normal/1003');
        figure[3] = utils.createSkeleton('other/role/normal/1004');
        figureHave[3] = utils.createSkeleton('other/role/normal/1004');
        var roleBoxall = new Laya.Box();
        roleBoxall.zOrder = 2;
        roleBoxall.width = 1100;
        roleBoxall.height = 700;
        roleBoxall.x = 135;
        roleBoxall.y = Laya.stage.height / 2 - roleBoxall.height / 2 + 50;
        roleBoxall.scrollRect = { x: 0, y: 0, width: 1100, height: 700 };
        this._roleBox = roleBoxall;
        this.addChild(roleBoxall);
        for (var i = 0; i < 4; i++) {
            this._choose[i] = figure[i];
            this._haveRole[i] = figureHave[i]
            this._choose[i].scale(0.5, 0.5);
            this._haveRole[i].scale(0.8, 0.8);
            this._choose[i].play(0, true, false, 0, 1);
            this._haveRole[i].play(0, true);
            this._role[i] = new ui.Role.RolePageUI();
            this._choose[i].pos(170, 310);
            this._role[i].scale(0.97, 0.97);
            this._role[i].roleBox.addChild(this._choose[i]);
            this._role[i].on(Laya.Event.MOUSE_UP, this, this.handleMouseUp);
            this._role[i].on(Laya.Event.MOUSE_DOWN, this, this.handleMouseDown);
            this._roleBox.addChild(this._role[i]);
            this._haveRole[i].pos(290, 520);
            this._role[i].mouseEnabled = true;
            this._role[i].on(Laya.Event.MOUSE_UP, this, this.handleMouseUp);
            this._role[i].on(Laya.Event.MOUSE_DOWN, this, this.handleMouseDown);
            this._roleBox.addChild(this._role[i]);
            this.ui.rightBtn.on(Laya.Event.CLICK, this, this.leftGo);
            this.ui.leftBtn.on(Laya.Event.CLICK, this, this.rightGo);
            this.ui.rightBtn.zOrder = 100;
            this.ui.leftBtn.zOrder = 100;
        }
        this.addChildAt(this._publicbg, 0);
    }
    _proto.changeData = function (data) {
        if (data.role.length < 1) { return; }
        for (var i = 0; i < 4; i++) {
            var starSU = [];
            var mEnergy = [];
            var mPower = [];
            var mSpeed = [];
            this._role[i].y = 25;
            this._status.push(data.role[i].status);
            if (data.role[i].status == 1) {
                this._choose[i].play(0, true);
                this._role[i].pos(363, 20);
                this.chooseChange(i);
                if (i == 0) {
                    this._role[1].x = 730;
                    this._role[2].x = -340;
                    this._role[3].x = 10;
                }
                else if (i == 1) {
                    this._role[0].x = 10;
                    this._role[2].x = 730;
                    this._role[3].x = -340;
                }
                else if (i == 2) {
                    this._role[0].x = -340;
                    this._role[1].x = 10;
                    this._role[3].x = 730;
                }
                else if (i == 3) {
                    this._role[0].x = 730;
                    this._role[1].x = -340;
                    this._role[2].x = 10;
                }
            }
            this._role[i].blistersList.repeatX = data.role[i].maxPower;
            this._role[i].columnList.repeatX = data.role[i].maxEnergy;
            this._role[i].speedList.repeatX = data.role[i].maxSpeed;
            this._roleId[i] = data.role[i].roleId;
            for (var j = 0; j < 5; j++) {
                if (j < data.role[i].star) {
                    starSU[j] = { 'trend': 1 };
                } else {
                    starSU[j] = { 'trend': 0 };
                }

            }
            for (var j = 0; j < data.role[i].maxEnergy; j++) {
                if (j < data.role[i].initEnergy) {
                    mEnergy[j] = { 'column': 1 };
                } else if (j >= data.role[i].initEnergy && j < data.role[i].energy) {
                    mEnergy[j] = { 'column': 0 };
                } else {
                    mEnergy[j] = { 'column': 2 };
                }
            }
            for (var j = 0; j < data.role[i].maxPower; j++) {
                if (j < data.role[i].initPower) {
                    mPower[j] = { 'blisters': 1 };
                } else if (j >= data.role[i].initPower && j < data.role[i].power) {
                    mPower[j] = { 'blisters': 0 };
                } else {
                    mPower[j] = { 'blisters': 2 };
                }
            }
            for (var j = 0; j < data.role[i].maxSpeed; j++) {
                if (j < data.role[i].initSpeed) {
                    mSpeed[j] = { 'speed': 1 };
                } else if (j >= data.role[i].initSpeed && j < data.role[i].speed) {
                    mSpeed[j] = { 'speed': 0 };
                } else {
                    mSpeed[j] = { 'speed': 2 };
                }
            }
            this._role[i].name.text = data.role[i].name;
            this._role[i].starList.array = starSU;
            this._role[i].blistersList.array = mPower;
            this._role[i].columnList.array = mEnergy;
            this._role[i].speedList.array = mSpeed;
            if (data.role[i].status == 0) {         //已拥有选择
                this._role[i].an_btn.visible = true;
                this._role[i].chooseBtn.visible = true;
                this._role[i].userIng.visible = false;
                this._role[i].debrisBtn.visible = false;
                this._role[i].domBtn.visible = false;
            } else if (data.role[i].status == 1) {      //已拥有使用中
                this._role[i].an_btn.visible = false;
                this._role[i].chooseBtn.visible = false;
                this._role[i].userIng.visible = true;
                this._role[i].debrisBtn.visible = false;
                this._role[i].domBtn.visible = false;
            }
            else if (data.role[i].status == 2) {       //钻石购买
                this._role[i].an_btn.visible = true;
                this._role[i].chooseBtn.visible = false;
                this._role[i].userIng.visible = false;
                this._role[i].debrisBtn.visible = false;
                this._role[i].domBtn.visible = true;
                this._role[i].domNum.text = data.role[i].openNum;
            }
            else if (data.role[i].status == 3) {       //碎片合成
                var roleIndex;
                this._role[i].an_btn.visible = true;
                this._role[i].chooseBtn.visible = false;
                this._role[i].userIng.visible = false;
                this._role[i].debrisBtn.visible = true;
                this._role[i].domBtn.visible = false;
                this._role[i].debrisNum.text = data.role[i].chip + '/' + data.role[i].openNum;
                if (i == 0) {
                    roleIndex = 1;
                } else if (i == 1) {
                    roleIndex = 0;
                } else if (i == 2) {
                    roleIndex = 3;
                }
                else if (i == 3) {
                    roleIndex = 2;
                }
                this._role[i].debrisChoose.index = roleIndex;
            }
        }
    }
    _proto.chooseChange = function (choose) {
        this._role[choose].scale(1, 1);
        this._role[choose].an_btn.visible = false;
        this._role[choose].people_bg.visible = false;
        this._role[choose].outline.visible = true;
        this._choose[choose].play(0, true);
    }
    _proto.chooseNormal = function (Normal) {
        this._choose[Normal].stop();
        this._role[Normal].scale(0.97, 0.97);
        this._role[Normal].an_btn.visible = true;
        this._role[Normal].people_bg.visible = true;
        this._role[Normal].outline.visible = false;
    }
    _proto.bindEvent = function () {
        this._role[0].roleBox.on(Laya.Event.CLICK, this, function () {
            if (this._role[0].x == 363) {
                ppt.expIndex = 0;
                ppt.director.runScene(new Scene.Roledetails());
            }

        });
        this._role[1].roleBox.on(Laya.Event.CLICK, this, function () {
            if (this._role[1].x == 363) {
                ppt.expIndex = 1;
                ppt.director.runScene(new Scene.Roledetails());
            }

        });
        this._role[2].roleBox.on(Laya.Event.CLICK, this, function () {
            if (this._role[2].x == 363) {
                ppt.expIndex = 2;
                ppt.director.runScene(new Scene.Roledetails());
            }
        });
        this._role[3].roleBox.on(Laya.Event.CLICK, this, function () {
            if (this._role[3].x == 363) {
                ppt.expIndex = 3;
                ppt.director.runScene(new Scene.Roledetails());
            }
        });
        this._role[0].haveBtn.on(Laya.Event.CLICK, this, function () {
            if (this._role[0].x == 363) {
                this.haveDeris(0);
                this._roleIndex = 0;
            }
        });
        this._role[1].haveBtn.on(Laya.Event.CLICK, this, function () {
            if (this._role[1].x == 363) {
                this.haveDeris(1);
                this._roleIndex = 1;
            }
        });
        this._role[2].haveBtn.on(Laya.Event.CLICK, this, function () {
            if (this._role[2].x == 363) {
                this.haveDeris(2);
                this._roleIndex = 2;
            }
        });
        this._role[3].haveBtn.on(Laya.Event.CLICK, this, function () {
            if (this._role[3].x == 363) {
                this.haveDeris(3);
                this._roleIndex = 3;
            }
        });
    }

    _proto.haveDeris = function (index) {     //角色按钮点击请求
        io.emit({ 'cmd': 'role::press', params: { 'roleId': this._roleId[index] } });
    }
    _proto.upData = function (data) {        //角色按钮点击请求结果
        if (data.code) {
            var config = {
                msg: data.msg,
                close: true,
                btn: {
                    btnCancel: false
                }
            }

            switch (data.code) {
                case 302:
                    config.btn = {
                        btnCancel: false,
                        btnConfirm: {
                            skin: "confirm",
                            click: function () {
                                ppt.director.runScene(new Scene.Shop);
                            }
                        }
                    }
                    break;
                case 303:
                    config.btn = {
                        btnCancel: {
                            skin: "challenge",
                            click: function () {
                                io.emit({ cmd: 'pve::wait', params: { type: 6 } });
                            }
                        },
                        btnConfirm: {
                            skin: "race",
                            click: function () {
                                ppt.director.runScene(new Scene.Matching());
                            }
                        }
                    }
                    break;
                case 321:
                    config.btn.btnConfirm = {
                        skin: "confirm",
                        click: function () {
                            io.emit({ cmd: 'pve::wait', params: { type: 6 } });
                        }
                    }
                    break;
                default:
                    config.btn.btnConfirm = {
                        skin: "confirm"
                    }
            }

            ppt.director.popScene(new Alert.Public(config));
            return;
        }
        var newRole;
        if (data.role.status == 0) {
            newRole = this._haveRole[this._roleIndex];
            ppt.director.popScene(new Alert.NewRole(newRole), { autoClose: 3000 });
            this._role[this._roleIndex].chooseBtn.visible = true;
            this._role[this._roleIndex].userIng.visible = false;
            this._role[this._roleIndex].debrisBtn.visible = false;
            this._role[this._roleIndex].domBtn.visible = false;
        } else if (data.role.status == 1) {   //使用中
            for (var i = 0; i < 4; i++) {
                if (i == this._roleIndex) {
                    this._role[i].userIng.visible = true;
                    this._role[i].chooseBtn.visible = false;
                    this._role[i].debrisBtn.visible = false;
                    this._role[i].domBtn.visible = false;
                } else {
                    if (this._role[i].userIng.visible == true) {
                        this._role[i].userIng.visible == false;
                        this._role[i].chooseBtn.visible = true;
                        this._role[i].debrisBtn.visible = false;
                        this._role[i].domBtn.visible = false;
                    }
                }
            }

        }
    }
    _proto.leftGo = function () {
        this.direction = 'next';
        this.fieldScroll();
    }
    _proto.rightGo = function () {
        this.direction = 'prev';
        this.fieldScroll();
    }
    //按下事件
    _proto.handleMouseDown = function () {
        this.beginPosition[0] = this.stage.mouseX;
        this.beginPosition[1] = this.stage.mouseY;
        this.direction = null;
    }
    //抬起事件
    _proto.handleMouseUp = function () {
        this.endPosition[0] = this.stage.mouseX;
        this.endPosition[1] = this.stage.mouseY;
        if (this.endPosition[0] > this.beginPosition[0]) {
            this.direction = 'prev';
        } else if (this.endPosition[0] < this.beginPosition[0]) {
            this.direction = 'next';
        }
        this.fieldScroll();

    }
    _proto.fieldScroll = function () {
        for (var i = 0; i < this._len; i++) {
            switch (this.direction) {
                case 'prev':
                    if (this._role[i].x == -340) {
                        Laya.Tween.to(this._role[i], { x: 10, y: 25 }, 300, Laya.Ease['linearIn']);
                        this.chooseNormal(i);
                    } else if (this._role[i].x == 10) {
                        Laya.Tween.to(this._role[i], { x: 363, y: 20 }, 300, Laya.Ease['linearIn']);
                        this.chooseChange(i);
                    } else if (this._role[i].x == 363) {
                        Laya.Tween.to(this._role[i], { x: 730, y: 25 }, 300, Laya.Ease['linearIn']);
                        this.chooseNormal(i);
                    } else if (this._role[i].x == 730) {
                        Laya.Tween.to(this._role[i], { x: 1500, y: 25 }, 300, Laya.Ease['linearIn']);
                        this.chooseNormal(i);
                    } else if (this._role[i].x == 1500) {
                        this.chooseNormal(i);
                        this._role[i].x = -340;
                        Laya.Tween.to(this._role[i], { x: 10, y: 25 }, 300, Laya.Ease['linearIn']);
                    }
                    break;
                case 'next':
                    if (this._role[i].x == -340) {
                        this.chooseNormal(i);
                        this._role[i].x = 1500;
                        Laya.Tween.to(this._role[i], { x: 730, y: 25 }, 300, Laya.Ease['linearIn']);
                    } else if (this._role[i].x == 10) {
                        Laya.Tween.to(this._role[i], { x: -340, y: 25 }, 300, Laya.Ease['linearIn']);
                        this.chooseNormal(i);
                    } else if (this._role[i].x == 363) {
                        Laya.Tween.to(this._role[i], { x: 10, y: 25 }, 300, Laya.Ease['linearIn']);
                        this.chooseNormal(i);
                    } else if (this._role[i].x == 730) {
                        Laya.Tween.to(this._role[i], { x: 363, y: 20 }, 300, Laya.Ease['linearIn']);
                        this.chooseChange(i);
                    } else if (this._role[i].x == 1500) {
                        Laya.Tween.to(this._role[i], { x: 730, y: 25 }, 300, Laya.Ease['linearIn']);
                        this.chooseNormal(i);
                    }
                    break;
            }

        }
    }
    _proto.onAdd = function () {
        this.createUI();
        this.addChild(ppt.header);
        this.bindEvent();
    }
    _proto.onEnter = function () {
        io.emit({ "cmd": "role::roleList", params: {} });

        ppt.header.enter("role");
    }
    _proto.onExit = function () {
        io.unregister("role::roleList", this.changeData);
        io.unregister("role::press");
        this.destroy(true);
    }
    _proto.onResize = function (height) {
        this.BG.x = 667;
        this.BG.y = height / 2;
        this._roleBox.y = height / 2 - this._roleBox.height / 2 + 50;
        this.ui.y = height / 2 - this.ui.height;
    }
})();